<!doctype html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Vue2-04-Axios案例</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
</head>
<style>
    * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
    }

    body {
        background-color: #222;
        color: snow;
    }

    table {
        margin: 1rem;
        border-collapse: collapse;

        th, td {
            border: 1px solid silver;
            text-align: center;
        }
    }
</style>
<body>
<div id="app">
    <table>
        <thead>
        <tr>
            <th>userId</th>
            <th>id</th>
            <th>title</th>
            <th>body</th>
        </tr>
        </thead>
        <tbody>
        <tr v-for="post in posts">
            <td>{{post.userId}}</td>
            <td>{{post.id}}</td>
            <td style="text-align: left; padding-left: .5rem;">{{post.title}}</td>
            <td style="text-align: left; padding-left: .5rem;">{{post.body}}</td>
        </tr>
        </tbody>
    </table>
</div>
<script>
    new Vue({
        el: '#app',
        data: {
            posts: [],
        },
        mounted() {
            axios.get('https://jsonplaceholder.typicode.com/posts').then((response) => {
                console.log(response.data);
                this.posts = response.data;
            });
        },
    });
</script>
</body>
</html>